package com.fan.txl.ui;

import android.Manifest;
import android.graphics.Bitmap;
import android.hardware.Camera;
import android.os.Vibrator;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;

import com.base.txl.utils.RxUtils;
import com.fan.txl.R;
import com.fan.txl.ui.base.BaseActivity;
import com.tbruyelle.rxpermissions2.RxPermissions;

import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;
import cn.bingoogolapple.qrcode.zxing.ZXingView;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.functions.Consumer;
import io.reactivex.observers.DisposableObserver;

public class ScannerUI extends BaseActivity implements QRCodeView.Delegate  {
    private QRCodeView mQRCodeView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scanner_ui);

        mQRCodeView = (ZXingView) findViewById(R.id.qrcodeView);
        mQRCodeView.setDelegate(this);
        final RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.request(Manifest.permission.CAMERA).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if (!aBoolean){
                    finish();
                }else{
                    Toast.makeText(ScannerUI.this, "摄像头权限正确", Toast.LENGTH_SHORT).show();
                }
            }
        });
        Observable.create(new ObservableOnSubscribe<Bitmap>() {
            @Override
            public void subscribe(ObservableEmitter<Bitmap> e) throws Exception {
                Bitmap result=QRCodeEncoder.syncEncodeQRCode("www.baidu.com",300);
                e.onNext(result);
            }
        }).compose(RxUtils.<Bitmap>applySchedulers(RxUtils.IO_ON_UI_TRANSFORMER))
                .subscribe(new DisposableObserver<Bitmap>() {
                    @Override
                    public void onNext(Bitmap bitmap) {
                        ImageView iv=(ImageView) findViewById(R.id.imageView);
                            iv.setImageBitmap(bitmap);
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {

                    }
                });


    }

    @Override
    public void onScanQRCodeSuccess(String result) {
        Toast.makeText(this, result, Toast.LENGTH_SHORT).show();
        vibrate();
        mQRCodeView.stopSpot();
    }

    @Override
    public void onScanQRCodeOpenCameraError() {
        Toast.makeText(this, "error", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onStart() {
        super.onStart();
        mQRCodeView.startCamera();
        //mQRCodeView.startCamera(Camera.CameraInfo.CAMERA_FACING_BACK);
        //mQRCodeView.showScanRect();
        mQRCodeView.startSpotAndShowRect();
    }

    @Override
    protected void onStop() {
        mQRCodeView.stopCamera();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        mQRCodeView.onDestroy();
        super.onDestroy();
    }

    private void vibrate() {
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }


}
